package com.tjnu.losca.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tjnu.losca.core.FileUtil;
import com.tjnu.losca.core.ResponseVO;
import com.tjnu.losca.form.LoginForm;
import com.tjnu.losca.pojo.Teacher;
import com.tjnu.losca.service.ITeacherService;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author losca
 * @since 2021-09-16
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    private ITeacherService teacherService;
    //文件上传工具
    @Autowired
    private FileUtil fileUtil;
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @GetMapping("/getAll")
    public ResponseVO<IPage> getAll(@RequestParam(defaultValue = "1") int pageNum,
                                    @RequestParam(defaultValue = "3") int pageSize) {
        Page<Teacher> page = new Page<>(pageNum, pageSize);
        return ResponseVO.success("成功", teacherService.page(page));
    }

    /**
     * @param pageNum
     * @param pageSize
     * @param search   根据参数查询老师
     * @return
     */
    @GetMapping("/getDataBySearch")
    public ResponseVO<IPage> getDataBySearch(@RequestParam(defaultValue = "1") int pageNum,
                                             @RequestParam(defaultValue = "3") int pageSize,
                                             @RequestParam(defaultValue = "") String search) {
        return ResponseVO.success("查询成功", teacherService.getTeacherBySearch(pageNum, pageSize, search.trim()));
    }

    @PostMapping("/save")
    public ResponseVO<Boolean> save(Teacher teacher,String rawPassword, MultipartFile file) throws InvalidBucketNameException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidPortException, InvalidKeyException, InvalidEndpointException, InvalidResponseException, XmlParserException, InternalException {
        //对密码进行加密
        teacher.setPassword(passwordEncoder.encode(rawPassword));
        teacher.setIcon(fileUtil.imgUpload(file));
        return ResponseVO.success("保存成功", teacherService.save(teacher));
    }

    @PostMapping("/update")
    public ResponseVO<Boolean> update(Teacher teacher,MultipartFile file) throws InvalidBucketNameException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidPortException, InvalidKeyException, InvalidEndpointException, InvalidResponseException, XmlParserException, InternalException {
        //头像不为空
        if (file != null){
            String s = fileUtil.imgUpload(file);
            teacher.setIcon(s);
        }
        return ResponseVO.success("修改成功",teacherService.updateById(teacher));
    }

    @GetMapping("/getTeacherById")
    public ResponseVO<Teacher> getTeacherById(Integer id) {
        return ResponseVO.success("成功", teacherService.getById(id));
    }

    @GetMapping("/del")
    public ResponseVO<Boolean> del(Integer id) {
        return ResponseVO.success("删除成功", teacherService.del(id));
    }

    /**
     * 恢复
     * @param
     * @return
     * @throws Exception
     */
    @GetMapping("/recovery")
    public ResponseVO<Boolean> recovery(Integer id){
        return ResponseVO.success("恢复成功",teacherService.recovery(id));
    }
    @PostMapping("/login")
    public ResponseVO<String> login(@RequestBody LoginForm form) throws Exception {
        return teacherService.login(form.getUsername(),form.getPassword());
    }

    /**
     * 验证手机号 不能重复
     * @return
     */
    @GetMapping("/check")
    public ResponseVO<Boolean> check(Integer id,String value){
        boolean check = teacherService.check(id,value);
        if (check){
            return ResponseVO.success("验证通过");
        }
        return ResponseVO.failed("手机号重复");
    }


}
